package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.SystemPost;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.web.admin.mapper.SystemUserMapper;
import com.atguigu.lease.web.admin.service.SystemPostService;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserItemVo;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liubo
 * @description 针对表【system_user(员工信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class SystemUserServiceImpl extends ServiceImpl<SystemUserMapper, SystemUser>
        implements SystemUserService {

    @Autowired
    private SystemUserMapper systemUserMapper;
    @Autowired
    private SystemPostService service;
    @Override
    public void pageSystemUser(IPage<SystemUserItemVo> page, SystemUserQueryVo queryVo) {
        systemUserMapper.pageSystemUser(page,queryVo);
    }

    @Override
    public SystemUserItemVo getSystemUserById(Long id) {
        SystemUser systemUser = getById(id);
        systemUser.setPassword(null);
        SystemPost systemPost = service.getById(systemUser.getPostId());
        SystemUserItemVo systemUserItemVo = new SystemUserItemVo();
        BeanUtils.copyProperties(systemUser,systemUserItemVo);
        systemUserItemVo.setPostName(systemPost.getName());
        return systemUserItemVo;
    }

    @Override
    public void customsaveOrUpdate(SystemUser systemUser) {
        //1.检查账号问题,如果为保存名字不可重复，更新名字则可以重复

        LambdaQueryWrapper<SystemUser> lambdaQueryWrapper = new LambdaQueryWrapper<SystemUser>()
                .ne(systemUser.getId() != null, SystemUser::getId, systemUser.getId())
                .eq(SystemUser::getUsername, systemUser.getUsername());
        long count = count(lambdaQueryWrapper);
        if (count>0){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_EXIST_ERROR);
        }
        //2.密码校验及加密
        if (!ObjectUtils.isEmpty(systemUser.getPassword())){
            systemUser.setPassword(DigestUtils.md5Hex(systemUser.getPassword()));
        }




//        //如果传入的ID不为空，是为更新修改
//        if (systemUser.getId()!=null){
//            LambdaQueryWrapper<SystemUser> userLambdaQueryWrapper = new LambdaQueryWrapper<SystemUser>().eq(SystemUser::getUsername, systemUser.getUsername());
//        }else {
//            //保存
//            LambdaQueryWrapper<SystemUser> userLambdaQueryWrapper = new LambdaQueryWrapper<SystemUser>().eq(SystemUser::getUsername, systemUser.getUsername());
//            long count = count(userLambdaQueryWrapper);
//            if (count>0){
//                throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_EXIST_ERROR);
//            }
//            //2.密码校验及加密
//            if (!ObjectUtils.isEmpty(systemUser.getPassword())){
//                systemUser.setPassword(DigestUtils.md5Hex(systemUser.getPassword()));
//            }
//        }


        //3.保存更新
        saveOrUpdate(systemUser);
    }
}




